package com.tem.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.ModelAndView;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import com.tem.common.tool.result.ResultUtil;
import org.springframework.beans.BeanUtils;
import com.tem.system.entity.SysCompany;
import com.api.manage.pojo.system.SysCompanyBean;
import com.tem.system.service.ISysCompanyService;
import com.tem.common.core.base.BaseController;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author 腾木
 * @since 2023-11-14
 */
@RestController
@RequestMapping("/system/sysCompany")
public class SysCompanyController extends BaseController{
    @Autowired
    private ISysCompanyService sysCompanyService;

    @ApiOperation(value = "列表")
    @RequestMapping("list")
    public ResultUtil<IPage<SysCompanyBean>> list(SysCompanyBean request, HttpServletRequest req){
        request.setSysCompanyId(this.getSysCompanyId(req));
        return this.init(request,req);
    }

    @ApiOperation(value = "系统构建-企业列表")
    @RequestMapping("init")
    public ResultUtil<IPage<SysCompanyBean>> init(SysCompanyBean request, HttpServletRequest req){
        QueryWrapper<SysCompany> wrapper = new QueryWrapper<>();
        if(request.getSysCompanyId()!=null){
            wrapper.lambda().eq(SysCompany::getSysCompanyId,request.getSysCompanyId());
        }
        if(request.getCompanyName()!=null && request.getCompanyName().trim().length()>0){
            wrapper.lambda().like(SysCompany::getCompanyName,request.getCompanyName().trim());
        }
        wrapper.orderByDesc("sys_company_id");
        Page page = new Page<>(request.getCurrent(),request.getSize());
        IPage listBody = sysCompanyService.page(page,wrapper);
        List<SysCompany> entityList=listBody.getRecords();
        if(!entityList.isEmpty()) {
            List<SysCompanyBean> responseList = new ArrayList<>();
            entityList.forEach(it -> {
                SysCompanyBean bean = new SysCompanyBean();
                BeanUtils.copyProperties(it,bean);
                responseList.add(bean);
            });
            listBody.setRecords(responseList);
        }
        return ResultUtil.success(listBody);
    }

    @PostMapping("save")
    @ApiOperation(value = "新增")
    public ResultUtil save(@RequestBody SysCompany entity){
        //entity.setCreateTime(LocalDateTime.now());entity.setDeleted(0);
        if(entity.getSysCompanyId()!=null){
            sysCompanyService.updateById(entity);
        }else{
            entity.setDeleted(0);
            sysCompanyService.save(entity);
        }
        return ResultUtil.success();
    }

    @GetMapping("getById/{id}")
    @ApiOperation(value = "id查询")
    public ResultUtil<SysCompanyBean> getById(@PathVariable Long id,HttpServletRequest req){
        if(id==null || id.longValue()<0){
            id = this.getSysCompanyId(req);
        }
        SysCompany entity = sysCompanyService.getById(id);
        if(entity!=null){
            SysCompanyBean bean = new SysCompanyBean();
            BeanUtils.copyProperties(entity,bean);
            return ResultUtil.success(bean);
        }
        return ResultUtil.error("没有找到对象");
    }

    @PutMapping("update")
    @ApiOperation(value = "修改")
    public ResultUtil update(@RequestBody SysCompany entity){
        sysCompanyService.updateById(entity);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable Long id){
        sysCompanyService.removeById(id);
        return ResultUtil.success();
    }
}